// pages/addressList/addressList.js
import Dialog from '@vant/weapp/dialog/dialog';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIphoneX: app.globalData.isIphoneX,
    fromType: '', // 页面来源
    addressList: [], // 地址列表
    goodsId: '', // 商品ID
    id: '' // 订单id
  },

  // 获取地址
  geilist() {
    wx.get("/api/address/list", res => {
      console.log('==== geilist ===', res)
      if (res.data.length > 0) {
        res.data.map(item => item.address = item.provinceName + item.cityName + item.countyName)
        this.setData({
          addressList: res.data
        })
      } else {
        this.setData({
          addressList: []
        })
      }
    }, fail => {})
  },

  // 返回订单确认页面
  backConfirm(e) {
    console.log(this.data)
    if (this.data.fromType == 'setAddress' && this.data.id) {
      wx.get(`/api/orders/modifyAddress/${this.data.id}?addressId=${e.currentTarget.dataset.id}`, res => {
        console.log('==== modifyAddress ===', res)
        if (res.code == 200) {
          wx.showToast({
            title: '地址修改成功',
            icon: 'none',
            duration: 1000
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        }
      }, fail => {})
    } else if (this.data.fromType == 'shopSetAddress' && this.data.id) {
      wx.get(`${wx.api.mallmodifyAddress}${this.data.id}?&goodsId=${this.data.goodsId}&addressId=${e.currentTarget.dataset.id}`, res => {
        console.log('==== modifyAddress ===', res)
        if (res.code == 200) {
          wx.showToast({
            title: '地址修改成功',
            icon: 'none',
            duration: 1000
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        }
      }, fail => {})
    } else if (this.data.fromType == 'yuSetAddress' && this.data.id) {
      wx.get(`${wx.api.presaletmodifyAddress}${this.data.id}?addressId=${e.currentTarget.dataset.id}`, res => {
        console.log('==== modifyAddress ===', res)
        if (res.code == 200) {
          wx.showToast({
            title: '地址修改成功',
            icon: 'none',
            duration: 1000
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        }
      }, fail => {})
    } else if (this.data.fromType == 'orderConfirm') {
      console.log(e.currentTarget.dataset.index)
      wx.setStorage({
        key: "addressSelect",
        data: e.currentTarget.dataset.index,
        success() {
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }
  },

  // 删除地址
  del(e) {
    console.log(e.currentTarget.dataset)
    let that = this
    Dialog.confirm({
        title: '温馨提示',
        message: '是否删除该地址',
      })
      .then(() => {
        // on confirm
        wx.get(`/api/address/${e.currentTarget.dataset.id}`, res => {
          console.log('==== address ===', res)
          if (res.code == 200) {
            that.geilist()
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },

  // 修改地址
  toUpd(e) {
    wx.setStorage({
      key: "addressItem",
      data: e.currentTarget.dataset.item,
      success() {
        wx.navigateTo({
          url: '/pages/addressAdd/addressAdd?id=' + e.currentTarget.dataset.item.id,
        })
      }
    })
  },

  // 添加地址
  toAdd() {
    wx.navigateTo({
      url: '/pages/addressAdd/addressAdd',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.info('==== options ====', options)
    if (options.fromType) {
      this.setData({
        fromType: options.fromType
      })
    }
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
    if (options.goodsId) {
      this.setData({
        goodsId: options.goodsId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.geilist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})